package com.zjlong.demo.ResideMenuUI;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zjlong.ResideMenu.ResideMenu;
import com.zjlong.ResideMenu.ResideMenuItem;
import com.zjlong.demo.CompanyApp.R;

public class MainMenuActivity extends FragmentActivity implements
		View.OnClickListener {

	private ResideMenu resideMenu;
	private MainMenuActivity mContext;
	private ResideMenuItem itemHome;
	private ResideMenuItem itemProfile;
	private ResideMenuItem itemCalendar;
	private ResideMenuItem itemSettings;

	private TextView fragmentHead;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_menu);
		mContext = this;
		setUpMenu();
		changeFragment(new HomeFragment());
	}

	private void setUpMenu() {

		// attach to current activity;
		resideMenu = new ResideMenu(this);
		// 主界面背景图片
		resideMenu.setBackground(R.drawable.init_image);
		resideMenu.attachToActivity(this);
		resideMenu.setMenuListener(menuListener);

		// create menu items;
		itemHome = new ResideMenuItem(this, R.drawable.icon_profile, "主页");
		itemProfile = new ResideMenuItem(this, R.drawable.icon_home, "店铺");
		itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar, "海报");
		itemSettings = new ResideMenuItem(this, R.drawable.icon_settings, "设置");

		itemHome.setOnClickListener(this);
		itemProfile.setOnClickListener(this);
		itemCalendar.setOnClickListener(this);
		itemSettings.setOnClickListener(this);

		resideMenu.addMenuItem(itemHome);
		resideMenu.addMenuItem(itemProfile);
		resideMenu.addMenuItem(itemCalendar);
		resideMenu.addMenuItem(itemSettings);

		findViewById(R.id.title_bar_menu).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						resideMenu.openMenu();
					}
				});
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return resideMenu.onInterceptTouchEvent(ev)
				|| super.dispatchTouchEvent(ev);
	}

	@Override
	public void onClick(View view) {

		if (view == itemHome) {
			// 设置分页标题
			fragmentHead = (TextView) findViewById(R.id.reside_item_text);
			fragmentHead.setText("主页");
			changeFragment(new HomeFragment());
		} else if (view == itemProfile) {
			fragmentHead = (TextView) findViewById(R.id.reside_item_text);
			fragmentHead.setText("店铺");
			changeFragment(new ProfileFragment());
		} else if (view == itemCalendar) {
			fragmentHead = (TextView) findViewById(R.id.reside_item_text);
			fragmentHead.setText("海报");
			changeFragment(new CalendarFragment());
		} else if (view == itemSettings) {
			fragmentHead = (TextView) findViewById(R.id.reside_item_text);
			fragmentHead.setText("设置");
			changeFragment(new SettingsFragment());
		}

		resideMenu.closeMenu();
	}

	private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
		@Override
		public void openMenu() {
			Toast.makeText(mContext, "Menu is opened!", Toast.LENGTH_SHORT)
					.show();
		}

		@Override
		public void closeMenu() {
			Toast.makeText(mContext, "Menu is closed!", Toast.LENGTH_SHORT)
					.show();
		}
	};

	private void changeFragment(Fragment targetFragment) {
		resideMenu.clearIgnoredViewList();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.main_fragment, targetFragment, "fragment")
				.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
				.addToBackStack(null).commit();
	}

	// What good method is to access resideMenu锛�
	public ResideMenu getResideMenu() {
		return resideMenu;
	}

}
